package com.ruoyi.course.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.LabCourse;
import com.ruoyi.course.domain.LabCourseAdjust;
import com.ruoyi.course.domain.LabCourseAdjustDetails;
import com.ruoyi.course.domain.LabCourseDate;
import com.ruoyi.course.mapper.LabCourseAdjustMapper;
import com.ruoyi.course.mapper.LabCourseDateMapper;
import com.ruoyi.course.mapper.LabCourseMapper;
import com.ruoyi.course.service.ILabCourseAdjustDetailsService;
import com.ruoyi.course.service.ILabCourseAdjustService;
import com.ruoyi.course.service.ILabCourseTimetableService;
import com.ruoyi.labdata.domain.LabFlowInstance;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.labdata.service.ILabHolidayReservationService;

/**
 * 调课管理Service业务层处理
 *
 * @author jun.wang
 * @date 2022-04-10
 */
@Service
public class LabCourseAdjustServiceImpl implements ILabCourseAdjustService {
	@Autowired
	private LabCourseAdjustMapper labCourseAdjustMapper;

	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;

	@Autowired
	private ILabCourseAdjustDetailsService labCourseAdjustDetailsService;

	@Autowired
	private LabCourseMapper labCourseMapper;

	@Autowired
	private ILabCourseTimetableService timetableService;

	@Autowired
	private LabCourseDateMapper labCourseDateMapper;
	
	@Autowired
	private ILabHolidayReservationService reservationService;

	/**
	 * 查询调课管理
	 *
	 * @param instanceId
	 *            调课管理ID
	 * @return 调课管理
	 */
	@Override
	public LabCourseAdjust selectLabCourseAdjustById(Long instanceId) {
		LabCourseAdjust labCourseAdjust = labCourseAdjustMapper.selectLabCourseAdjustById(instanceId);
		LabFlowInstance labFlowInstance = labFlowInstanceService.selectLabFlowInstanceById(instanceId);
		labCourseAdjust.setFlowName(labFlowInstance.getFlowName());
		labCourseAdjust.setCopyToUsers(labFlowInstance.getCopyToUsers());
		labCourseAdjust.setFlowStatusOpt(labFlowInstance.getFlowStatusOpt());

		setLabCourseAdjustDetail(labCourseAdjust);

		return labCourseAdjust;
	}

	private void setLabCourseAdjustDetail(LabCourseAdjust labCourseAdjust) {
		Long instanceId = labCourseAdjust.getInstanceId();

		LabCourseAdjustDetails detailsQuery = new LabCourseAdjustDetails();
		detailsQuery.setAdjustId(instanceId);
		List<LabCourseAdjustDetails> labCourseAdjustDetails = labCourseAdjustDetailsService
				.selectLabCourseAdjustDetailsList(detailsQuery);
		for (LabCourseAdjustDetails detail : labCourseAdjustDetails) {
			Long courseId = detail.getCourseId();
			LabCourse course = labCourseMapper.selectLabCourseById(courseId);
			if(detail.getLabId()==null){
				throw new RuntimeException("课程明细没有找到实验室ID");
			}
			course.setLabId(detail.getLabId());
			course.setClassRoomId(detail.getClassRoomId());
			course.setWeekOpenNums(detail.getWeekOpenNums());
			/*course.setWeekDayOpt(detail.getWeekDayOpt());
			course.setSectionStart(detail.getSectionStart());
			course.setSectionEnd(detail.getSectionEnd());
			course.setInitWeekStart(course.getWeekStart());
			course.setInitWeekEnd(course.getWeekEnd());
			course.setWeekStart(detail.getWeekStart());
			course.setWeekEnd(detail.getWeekEnd());
			course.setCourseDate(detail.getCourseDate());
			course.setCourseDateId(detail.getCourseDateId());*/
			course.setDay1(detail.getDay1());
			course.setDay2(detail.getDay2());
			course.setDay3(detail.getDay3());
			course.setDay4(detail.getDay4());
			course.setDay5(detail.getDay5());
			course.setDay6(detail.getDay6());
			course.setDay7(detail.getDay7());
			if (detail.getType().intValue() == 1) {
				labCourseAdjust.setCourse1(course);
			} else {
				labCourseAdjust.setCourse2(course);
			}
		}

	}

	/**
	 * 查询调课管理列表
	 *
	 * @param labCourseAdjust
	 *            调课管理
	 * @return 调课管理
	 */
	@Override
	public List<LabCourseAdjust> selectLabCourseAdjustList(LabCourseAdjust labCourseAdjust) {
		return labCourseAdjustMapper.selectLabCourseAdjustList(labCourseAdjust);
	}

	/**
	 * 新增调课管理
	 *
	 * @param labCourseAdjust
	 *            调课管理
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabCourseAdjust(LabCourseAdjust labCourseAdjust) {
		int type = labCourseAdjust.getType().intValue();
		validate(labCourseAdjust, type);
		labCourseAdjust.setCreateBy(SecurityUtils.getUsername());
		labCourseAdjust.setCreateTime(DateUtils.getNowDate());
		labCourseAdjust.setCreateOrgId(SecurityUtils.getLoginUser().getUser().getDeptId());
		labFlowInstanceService.insertLabFlowInstance(labCourseAdjust);
		insertLabCourseAdjustDetail(labCourseAdjust);
		return labCourseAdjustMapper.insertLabCourseAdjust(labCourseAdjust);
	}

	private void validate(LabCourseAdjust labCourseAdjust, int type) {
		if(type==2){
			if(labCourseAdjust.getCourse1().getWeekOpenNums()!=labCourseAdjust.getCourse1().getWeekOpenNums()){
				throw new RuntimeException("课表调整的周次必须相等.");
			}
		}
		/*List<Date> dates = timetableService.getDates(labCourseAdjust.getCourse1());
		for(Date date : dates){
			labCourseAdjust.getCourse1().setCourseDate(date);
			int exsitsCount1 = labCourseDateMapper.exsitsLabCourseDate(labCourseAdjust.getCourse1());
			if (exsitsCount1 > 0) {
				throw new RuntimeException("该实验室的时段已经被占用，不能被调整");
			}
			if (type == 2) {
				labCourseAdjust.getCourse2().setCourseDate(date);
				int exsitsCount2 = labCourseDateMapper.exsitsLabCourseDate(labCourseAdjust.getCourse2());
				if (exsitsCount2 > 0) {
					throw new RuntimeException("该实验室的时段已经被占用，不能被调整");
				}
			}
		}*/
	}

	private void insertLabCourseAdjustDetail(LabCourseAdjust labCourseAdjust) {
			Long type = labCourseAdjust.getType();
			LabCourse course1 = labCourseAdjust.getCourse1();
			LabCourseAdjustDetails details1 = new LabCourseAdjustDetails();
			BeanUtils.copyProperties(course1, details1);
			details1.setId(null);
			details1.setAdjustId(labCourseAdjust.getInstanceId());
			details1.setType(1L);
			/*int exsitsCount1 = labCourseAdjustDetailsService.existsCourseAdjust(details1);
			if (exsitsCount1 > 0) {
				throw new RuntimeException("该实验室对应的时段已经被占用，不能被调整");
			}*/
			labCourseAdjustDetailsService.insertLabCourseAdjustDetails(details1);
			if(type.intValue() == 1){
				//课程调整预先占用预约
				course1.setCourseDateList(timetableService.getDates(course1));
				reservationService.doReservation(course1);
			}
			if (type.intValue() == 2) {
				LabCourse course2 = labCourseAdjust.getCourse2();
				LabCourseAdjustDetails details2 = new LabCourseAdjustDetails();
				BeanUtils.copyProperties(course2, details2);
				details2.setId(null);
				details2.setAdjustId(labCourseAdjust.getInstanceId());
				details2.setType(2L);
				/*exsitsCount1 = labCourseAdjustDetailsService.existsCourseAdjust(details2);
				if (exsitsCount1 > 0) {
					
					throw new RuntimeException("该实验室对应的时段已经被占用，不能被调整");
				}*/
				labCourseAdjustDetailsService.insertLabCourseAdjustDetails(details2);
			}
		
	}

	/**
	 * 修改调课管理
	 *
	 * @param labCourseAdjust
	 *            调课管理
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabCourseAdjust(LabCourseAdjust labCourseAdjust) {
		int type = labCourseAdjust.getType().intValue();
		validate(labCourseAdjust, type);
		//取消之前的预定设置
		if(type==1){
			LabCourseAdjust oldLabCourseAdjust = this.selectLabCourseAdjustById(labCourseAdjust.getInstanceId());
			LabCourse course1 = oldLabCourseAdjust.getCourse1();
			course1.setCourseDateList(timetableService.getDates(course1));
			reservationService.cancelReservation(course1);
		}
		labFlowInstanceService.updateLabFlowInstance(labCourseAdjust);
		updateLabCourseAdjustDetail(labCourseAdjust);
		return labCourseAdjustMapper.updateLabCourseAdjust(labCourseAdjust);
	}

	private void updateLabCourseAdjustDetail(LabCourseAdjust labCourseAdjust) {
		Long instanceId = labCourseAdjust.getInstanceId();
		labCourseAdjustDetailsService.deleteLabCourseAdjustDetailsByCourseAdjustId(instanceId);
		insertLabCourseAdjustDetail(labCourseAdjust);
	}

	/**
	 * 批量删除调课管理
	 *
	 * @param instanceIds
	 *            需要删除的调课管理ID
	 * @return 结果
	 */
	@Override
	public int deleteLabCourseAdjustByIds(Long[] instanceIds) {
		return labCourseAdjustMapper.deleteLabCourseAdjustByIds(instanceIds);
	}

	/**
	 * 删除调课管理信息
	 *
	 * @param instanceId
	 *            调课管理ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabCourseAdjustById(Long instanceId) {
		labCourseAdjustDetailsService.deleteLabCourseAdjustDetailsByCourseAdjustId(instanceId);
		return labCourseAdjustMapper.deleteLabCourseAdjustById(instanceId);
	}

	@Override
	@Transactional
	public int submitLabApplyOccupation(LabCourseAdjust labCourseAdjust) {
		this.updateLabCourseAdjust(labCourseAdjust);
		return labFlowInstanceService.commitFlow(labCourseAdjust);
	}

	@Override
	@Transactional
	public void commitCourseAdjust(Long instanceId) {
		LabCourseAdjust labCourseAdjust = selectLabCourseAdjustById(instanceId);
		Long type = labCourseAdjust.getType();
		Boolean isFinish = labCourseAdjust.getFlowStatusOpt().equals("finished");
		LabCourse course1 = labCourseAdjust.getCourse1();
		LabCourse course2 = labCourseAdjust.getCourse2();
		if (type.intValue() == 1) {
			if(isFinish){
				//取消课程之前的实验室预定
				LabCourse oldCourse1 = labCourseMapper.selectLabCourseById(course1.getCourseId());
				oldCourse1.setWeekStart(course1.getWeekStart());
				oldCourse1.setWeekEnd(course1.getWeekEnd());
				oldCourse1.setCourseDateList(timetableService.getDates(oldCourse1));
				reservationService.cancelReservation(oldCourse1);
				updateCourseDate(course1);
			}else{
				course1.setCourseDateList(timetableService.getDates(course1));
				reservationService.cancelReservation(course1);
			}
			
		} else {
			if(isFinish){
				updateCourseDate(course1);
				updateCourseDate(course2);
			}
		}
		
	}

	private void updateCourseDate(LabCourse course1) {
		//LabCourseDate query = new LabCourseDate();
		//query.setCourseId(course1.getCourseId());
		labCourseDateMapper.deleteLabCourseDateByCourseId(course1.getCourseId());
		List<LabCourseDate> courseDateList = timetableService.getDates(course1);
		for (int i = 0; i < courseDateList.size(); i++) {
			LabCourseDate labCourseDate = courseDateList.get(i);
			labCourseDate.setLabId(course1.getLabId());
			labCourseDate.setClassRoomId(course1.getClassRoomId());
			labCourseDate.setCreateBy(course1.getCreateBy());
			labCourseDate.setCourseName(course1.getCourseName()+"/"+course1.getExperimentalProject());
			labCourseDate.setClassNo(course1.getClassNo());
			labCourseDate.setNickName(course1.getCreateBy());
			labCourseDate.setUserNum(course1.getPersonNum());
			labCourseDate.setWeekNum(course1.getWeekOpenNums());
			labCourseDate.setTimetableId(course1.getTimetableId());
			labCourseDateMapper.insertLabCourseDate(labCourseDate);
		}
	}
}
